package com.example.newspapersmanageos.Service;

import com.example.newspapersmanageos.Entity.*;
import com.example.newspapersmanageos.Mapper.NewspaperMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UploadService {
    @Autowired
    NewspaperMapper newspaperMapper;

    //保存报刊信息
    public String addNewspaperInfo(Newspaper newspaper) {
        String result = "保存成功";
        int i = newspaperMapper.addNewspaperInfo(newspaper);
        result = i == 1 ? "保存成功" : "保存失败";
        return result;
    }
    //获取所有报刊信息
    public Page<ShowInfo> getAllNewspaperInfo(int pageNum, int pageSize){
        Page<ShowInfo> page = new Page(pageNum, pageSize);
        int count = newspaperMapper.countByPage();
        int parameter = (pageNum-1)*pageSize;
        List<Newspaper> n = newspaperMapper.getAllNewspaper(parameter, pageSize);
        List<Title> t = newspaperMapper.getTitleById(n.get(0).getId());
        List<ShowInfo> showInfos = new ArrayList<>();
        ShowInfo s = new ShowInfo(n,t);
        showInfos.add(s);
        page.setList(showInfos);
        page.setTotalCount(count);
        return page;
    }
    //添加标题信息
    public String addTitleInfo(Title title){
        String result = "保存成功";
        int i = newspaperMapper.addTitleInfo(title);
        result = i == 1 ? "保存成功" : "保存失败";
        return result;
    }
    //根据图片名称查询报刊id
    public int getIdByImgName(String pictureName){
        int result = newspaperMapper.getIdByImgName(pictureName);
        return result;
    }

}
